package engine.ui
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.event.CustomEvent;
	import engine.event.SoundEvent;
	import engine.flag.GameDefine;
	import engine.logic.walk.RollerMoveLogic;
	import engine.logic.walk.WalkLogic;
	import engine.logic.walk.WalkManager;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.RightTopIndexLogic;
	import engine.scene.SceneManager;
	import engine.scene.ScenePlayerLogic;
	import engine.scene.player.BasePlayerModel;
	import engine.scene.player.NpcModel;
	import engine.scene.player.PlayerModel;
	import engine.scene.player.RoleModel;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.radio.NxRadioEvent;
	import newx.com.control.radio.NxRadioGroup;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;
	
	public class SystemConfigureUI extends BaseUI
	{
		
		public var	text_bg				:MovieClip
		public var	music_title			:NxLabel
		public var	music_titel_1		:NxLabel
		public var	music_titel_2		:NxLabel
		public var	scenemove_title		:NxLabel
		public var	scenemove_title_2	:NxLabel
		public var	scenemove_title_1	:NxLabel
		public var	playerview_title	:NxLabel
		public var	player_title_1		:NxLabel
		public var	player_title_2		:NxLabel
		public var	Btn_begin			:NxButton
		public var	Btn_over			:NxButton
		public var	music_on			:NxRadio
		public var	music_off			:NxRadio
		public var	move_on				:NxRadio
		public var	move_off			:NxRadio
		public var	play_on				:NxRadio
		public var	play_off			:NxRadio
		public var  exitGame			:NxButton;
		
		public var	quality_title		:NxLabel
		public var	quality_title_1		:NxLabel
		public var	quality_title_2		:NxLabel
		public var	quality_low			:NxRadio
		public var	quality_high		:NxRadio
		
		private var musicArray			:Array;
		private var moveArray			:Array;
		private var playArray			:Array;
		private var qualityArray		:Array;
		public var musicGroup			:NxRadioGroup;
		private var moveGroup			:NxRadioGroup;
		public var playGroup			:NxRadioGroup;
		private var qualityGroup		:NxRadioGroup;
		
		private var _model:ModelLocator;
		
		private static var _self:SystemConfigureUI;
		
		public function SystemConfigureUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		public static	function getInstance():SystemConfigureUI
		{
			if(_self == null)
			{
				_self	=	new SystemConfigureUI();
			}
			return _self;
		}
		override public function Init():void
		{
			musicArray	=	[music_on,music_off];
			moveArray	=	[move_on,move_off];
			playArray	=	[play_on,play_off];
			qualityArray=	[quality_low,quality_high];
			
			musicGroup	=	new NxRadioGroup(musicArray);
			moveGroup	=	new NxRadioGroup(moveArray);
			playGroup	=	new NxRadioGroup(playArray);
			qualityGroup=	new NxRadioGroup(qualityArray);
			musicGroup.selectedIndex	=	0;
			moveGroup.selectedIndex		=	0;
			playGroup.selectedIndex		=	0;
			qualityGroup.selectedIndex	=	0;
			
			//--------------去除人物移动设定功能，要的时候显示出来就好了
			scenemove_title.visible	=	move_on.visible	=	scenemove_title_1.visible = move_off.visible =	scenemove_title_2.visible	=	false
			scenemove_title.mouseEnabled	=	move_on.mouseEnabled	=	scenemove_title_1.mouseEnabled = move_off.mouseEnabled =	scenemove_title_2.mouseEnabled	=	false
				
			_model = ModelLocator.getInstance();
			_model.addEventListener( CustomEvent.SWITCH_SCENE, function(e:CustomEvent):void
			{
				var userdata:SharedObject	=	SharedObject.getLocal("userdata");
//				if(userdata.data.playGroupIndex)
//				{
					if(GameData.SceneType == GameDefine.SceneType_FactionScene)
					{
						SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible(true);
					}
					else
					{
						SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible(userdata.data.playGroupIndex==0);
					}
//				}
			}
			);
		}
		
		private function music(val:int):void
		{
			if(val == 0)
			{
				_model.dispatchEvent(new SoundEvent(SoundEvent.MUSIC_ON));

			}
			else
			{
				_model.dispatchEvent(new SoundEvent(SoundEvent.MUSIC_OFF));
			}
		}
		private function scence(val:int):void
		{
			if(val == 0)
			{
				RollerMoveLogic.MoveType	=	1;
			}
			else
			{
				RollerMoveLogic.MoveType	=	-1;
			}
//			SceneManager.getInstance().setSceneViewRect();
		}
		private function playNum(val:int):void
		{
			SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible( val == 0 );
			RightTopIndexLogic.getInstance().changeDisplayShow(val==0?true:false);
		}
		private function quality(val:int):void
		{
//			WalkManager.getInstance().walkLogic.moveTimer = val == 1 ? null : new Timer(10);
			
			if( val == 1 )
			{//低配
//				NpcModel.chaseSpeed = 10;
//				NpcModel.moveSpeed = 3;
//			    GameData.Role.moveLogic.speed = 10;
				WalkManager.getInstance().walkLogic.magnificationCount = 2;
				ScenePlayerLogic._playerMax = 10;
			}else
			{
//				GameData.Role.moveLogic.speed = 8;
//				NpcModel.chaseSpeed = 10;
//				NpcModel.moveSpeed = 3;
				WalkManager.getInstance().walkLogic.magnificationCount = 4
				ScenePlayerLogic._playerMax = 30;
			}
			
//			var players : Array = SceneManager.getInstance().norSceneView.scenePlayerLogic.playerDepts;
//			
//			for each( var player : BasePlayerModel in players )
//			{
//				if( player is PlayerModel )
//				{//玩家
//					player.moveLogic.speed = val == 1 ? 10 : 14; 
////					player.moveLogic.speed  = 17;
//				}else if( player is NpcModel )
//				{//npc
////					player.moveLogic.speed = 20;
//					player.moveLogic.speed = val == 1 ? 3 : 10;
//				}
//			}
		}
		private function clickHandle(e:MouseEvent):void
		{
			if(e.target	==	Btn_begin)
			{
				musicGroup.selectedIndex	=	0;
				moveGroup.selectedIndex		=	0;
				playGroup.selectedIndex		=	0;
				music(0);
				scence(0);
				playNum(0);
			}
			else if(e.target	==	Btn_over)
			{
				saveInfo();
				hide();
			}
			else if (e.target	==	exitGame)
			{
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("UIStr_1001759"),AlertUI.OK|AlertUI.CANCEL,alertClickHandle)
			}
		}
		
		private function saveInfo():void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			userdata.data.musicGroupIndex	=	musicGroup.selectedIndex;
			userdata.data.moveGroupIndex	=	moveGroup.selectedIndex;
			userdata.data.playGroupIndex	=	playGroup.selectedIndex;
			userdata.data.qualityGroupIndex	=	qualityGroup.selectedIndex;
		}
		
		/**
		 * 卡号自救
		 * @param	type
		 * @param	str
		 * @param	clickData
		 */
		private function alertClickHandle(type:int, str:String, clickData:*):void
		{
			if(AlertUI.OK == type )
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_KICK_SELF);
			}
		}
		private function radioGroupClick(e:NxRadioEvent):void
		{
			if(e.target	==	musicGroup)
			{
				music(e.target.selectedIndex);
			}
			else if(e.target	==	moveGroup)
			{
				scence(e.target.selectedIndex);
			}
			else if(e.target	==	playGroup)
			{
				playNum(e.target.selectedIndex);
			}else if(e.target == qualityGroup)
			{
				quality(e.target.selectedIndex);
			}
		}

		private function onClickClose(e:NxWindowEvent):void
		{
			saveInfo();
			hide();
		}
		override protected function BeforeShow():void
		{
			Btn_begin	.addEventListener(MouseEvent.CLICK, clickHandle);
			Btn_over	.addEventListener(MouseEvent.CLICK, clickHandle);
			musicGroup	.addEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick)
			moveGroup	.addEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick)
			playGroup	.addEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick)
			windowBG	.addEventListener(NxWindowEvent.WINDOW_CLOSE, onClickClose);
			exitGame	.addEventListener(MouseEvent.CLICK, clickHandle);
			qualityGroup.addEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick);
			
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			if(userdata.data.musicGroupIndex)
			{
				musicGroup.selectedIndex	=	userdata.data.musicGroupIndex;
			}
			if(userdata.data.moveGroupIndex)
			{
				moveGroup.selectedIndex		=	userdata.data.moveGroupIndex;
			}
			if(userdata.data.playGroupIndex)
			{
				playGroup.selectedIndex		=	userdata.data.playGroupIndex;
			}
			if(userdata.data.qualityGroup)
			{
				qualityGroup.selectedIndex		=userdata.data.qualityGroupIndex;
			}
		}
		
		override protected function AfterHide():void
		{
			Btn_begin	.removeEventListener(MouseEvent.CLICK, clickHandle);
			Btn_over	.removeEventListener(MouseEvent.CLICK, clickHandle);
			exitGame	.removeEventListener(MouseEvent.CLICK, clickHandle);
			musicGroup	.removeEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick)
			moveGroup	.removeEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick)
			playGroup	.removeEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick)
			qualityGroup.removeEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioGroupClick);
			windowBG	.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClickClose);
			saveInfo();
		}
	}
}